<?php
namespace app\common\model;
use AlibabaCloud\DBFS\V20200418\DbfsRecord;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\facade\Env;
use think\Model as ThinkModel;
use think\Db;

//会员列表模型
class ViperCardSupplement extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__QT_VIPER_CARD_SUPPLEMENT__';
    protected $name = 'qt_viper_card_supplement';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    /**
     * 打印手机结账账单
     */
    public static function getExcelData($excelName,$userInfo)
    {
        $fileName = Env::get('root_path').'public/uploads/excel/'.$excelName;
        $objPHPExcel = IOFactory::load($fileName);
        //默认选中sheet0表
        $sheetSelected = 0;
        $objPHPExcel->setActiveSheetIndex($sheetSelected);
        //获取表格行数
        $rowCount = $objPHPExcel->getActiveSheet()->getHighestRow();
        //获取表格列数
        $columnCount = $objPHPExcel->getActiveSheet()->getHighestColumn();
        $dataArr = array();
        /* 循环读取每个单元格的数据 */
        //行数循环
        for ($row = 2; $row <= $rowCount -1; $row++) {
            //列数循环 , 列数是以A列开始
            for ($column = 'A'; $column <= $columnCount; $column++) {
                $dataArr[$row -2][] = $objPHPExcel->getActiveSheet()->getCell($column . $row)->getValue();
            }
        }
        $error = [];

        foreach ($dataArr as $key=>$value){
            if($value['4']=='男'){
                $gender = '1';
            }elseif($value['4']=='女'){
                $gender = '2';
            }else{
                $gender = '1';
            }
            $cardType = Db::name('qt_viper_type')->where('name',$value[5])->value('id');
            if($cardType == null){
                $value['message'] = ($key+1)."行".'会员卡类型不正确';
                $error[] = $value;
            }elseif($value[7] == null){
                $value['message'] = ($key+1)."行".'新卡号没有填写';
                $error[] = $value;
            }elseif($value[0] == null){
                $value['message'] = ($key+1)."行".'老卡号没有填写';
                $error[] = $value;
            }elseif($value[2] == null){
                $value['message'] = ($key+1)."行".'手机号没有填写';
                $error[] = $value;
            }elseif($value[3] == null){
                $value['message'] = ($key+1)."行".'姓名没有填写';
                $error[] = $value;
            }elseif($value[8] == null){
                $value['message'] = ($key+1)."行".'新卡内码没有填写';
                $error[] = $value;
            } elseif($value[7] == null){
                $value['message'] = ($key+1)."行".'新卡卡号没有填写';
                $error[] = $value;
            }else{
                $birthday = null;
                $birthday_number = null;
                if($value[6] != null){
                    $birthday = strtotime($value[6]);
                    $birthday_number = date('m',$birthday).date('d',$birthday);
                }
                $salt = number_rand();
                if($value[11] != null){
                    $passwd = pwd($value[11],$salt);
                }else{
                    $passwd = pwd(config('default_passwd'),$salt);
                }

                $insertCardData = [
                    'store_id'=>$userInfo['store_id'],
                    'card_no'=>$value[7],
                    'mobile'=>$value[2],
                    'name'=>$value[3],
                    'gender'=>$gender,
                    'viper_type_id'=>$cardType,
                    'birthday'=>$birthday,
                    'birthday_number'=>$birthday_number,
                    'amount'=>$value[1],
                    'give_amount'=>$value[10],
                    'give_total_amount'=>$value[10],
                    'total_amount'=>$value[1],
                    'passwd'=>$passwd,
                    'salt'=>$salt,
                    'score'=>$value[9],
                    'status'=>1,
                    'neima'=>$value[8],
                    'open_content'=>$value[12],
                    'create_time'=>time(),
                ];
                $viperCardId = Db::name('qt_viper_card')->insertGetId($insertCardData);
                $supplementData = [
                    'card_id'=>$viperCardId,
                    'new_card_no'=>$value[7],
                    'viper_type_id'=>$cardType,
                    'money'=>$value[1],
                    'give_money'=>$value[10],
                    'old_card_no'=>$value[0],
                    'user_id'=>$userInfo['id'],
                    'create_time'=>time(),
                ];
                self::create($supplementData);
            }
        }
        return $error;
    }

}